package com.baiqian.doupo.container;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import org.apache.logging.log4j.LogManager;

public class NaJieSmallContainer extends Container {

    private final Inventory inventory;
    private final ItemStack itemStack;
    private final PlayerInventory playerInventory;

    protected NaJieSmallContainer(int id, PlayerInventory playerInventory, Inventory inventory, ItemStack itemStack) {
        super(ContainerTypeRegistry.naJieSmallContainer.get(), id);

        this.playerInventory = playerInventory;
        this.inventory = inventory;
        this.itemStack = itemStack;
        layoutNaJieSMallInventorySlots(inventory, 8, 8);
        layoutPlayerInventorySlots(playerInventory, 8, 84);
    }

    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        return true;
    }

    @Override
    public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
        return super.slotClick(slotId, dragType, (slotId>54&&playerInventory.getStackInSlot(slotId-54).
                equals(itemStack))?ClickType.QUICK_MOVE:clickTypeIn, player);
    }

    @Override
    public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
        return ItemStack.EMPTY;
    }

    @Override
    public void onContainerClosed(PlayerEntity playerIn) {
        CompoundNBT nbt = new CompoundNBT();
        ListNBT listNBT = inventory.write();
        int j = 0;
        for (int i = 0; i < 27; ++i) {
            if (!inventory.getStackInSlot(i).isEmpty()) {
                LogManager.getLogger().info("i:" + i);
                nbt.put("item" + j, listNBT.get(j));
                nbt.putInt("where" + j, i);
                ++j;
            }
        }
        nbt.putInt("num", listNBT.size());
        itemStack.setTag(nbt);
        super.onContainerClosed(playerIn);
    }

    private int addSlotRange(IInventory inventory, int index, int x, int y, int amount, int dx) {
        for (int i = 0; i < amount; i++) {
            addSlot(new Slot(inventory, index, x, y)
//            {
//                @Override
//                public boolean isItemValid(ItemStack stack) {
//                    return !stack.getItem().equals(ItemRegistry.na_jie_small.get());
//                }
//            }
            );
            x += dx;
            index++;
        }
        return index;
    }

    private int addSlotBox(IInventory inventory, int index, int x, int y, int horAmount, int dx, int verAmount, int dy) {
        for (int j = 0; j < verAmount; j++) {
            index = addSlotRange(inventory, index, x, y, horAmount, dx);
            y += dy;
        }
        return index;
    }

    private void layoutNaJieSMallInventorySlots(IInventory inventory, int leftCol, int topRow) {
        // NaJieSmall inventory
        addSlotBox(inventory, 0, leftCol, topRow, 9, 18, 3, 18);
    }

    private void layoutPlayerInventorySlots(IInventory playerInventory, int leftCol, int topRow) {
        // Player inventory
        addSlotBox(playerInventory, 9, leftCol, topRow, 9, 18, 3, 18);

        // Hotbar
        topRow += 58;
        addSlotRange(playerInventory, 0, leftCol, topRow, 9, 18);
    }
}

